package com.xunwulian.dubbo.service;

import com.xunwulian.base.constant.ConstantBean;
import com.xunwulian.base.dao.CardInfoDao;
import com.xunwulian.base.entity.CardInfoEntity;
import com.xunwulian.base.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
public class InnerCardInfoService {

    private static final Logger logger = LoggerFactory.getLogger(InnerCardInfoService.class);

    @Autowired
    private CardInfoDao cardInfoDao;

    public InnerCardInfoService() {
    }

    public CardInfoEntity loadCardInfoByMsisdn(String msisdn) {
        return cardInfoDao.getByMsisdn(msisdn);
    }

    /**
     * 添加单个卡号
     * @param msisdn
     * @param appId
     * @param user
     */
    public void addMsisdn(String msisdn, String appId, User user) {

        // 更新如下字段：卡号	卡号备注	IMSI	ICCID	用户状态	工作状态	本月累计使用流量(MB)	本月累计使用短信(条)
        CardInfoEntity existCardInfo = loadCardInfoByMsisdn(msisdn);
        if (existCardInfo != null) {
            // 卡号已经存在
            existCardInfo.setAppId(appId);
            existCardInfo.setUserId(user.getUSER_ID());
            existCardInfo.setUserName(user.getUSERNAME());
            updateCardInfo4Import(existCardInfo);
            logger.info("更新卡号msisdn:" + msisdn + ", appId:" + appId + ", username:" + user.getUSERNAME());
        } else {

            CardInfoEntity info = new CardInfoEntity();

            info.setMsisdn(msisdn);
            info.setAppId(appId);
            info.setUserId(user.getUSER_ID());
            info.setUserName(user.getUSERNAME());

            // 默认用户激活日期
            info.setUserOpenDate(ConstantBean.DEFAULT_DATE);

            addCardInfo4Import(info);
            logger.info("添加卡号msisdn:" + info.getMsisdn() + ", appId:" + appId + ", username:" + user.getUSERNAME());
        }

    }


    public void addCardInfo4Import(CardInfoEntity cardInfoEntity) {
        cardInfoDao.insert4Import(cardInfoEntity);
    }

    public void updateCardInfo4Import(CardInfoEntity cardInfoEntity) {
        cardInfoDao.update4Import(cardInfoEntity);
    }
}
